package com.mvvm.library.app;


import android.app.Activity;
import android.app.Application;

import com.mvvm.library.XBaseRetrofitConfig;
import com.mvvm.library.util.ActivityUtil;
import com.mvvm.library.util.LogUtil;
import com.mvvm.library.util.XUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @创建时间：2023/2/27
 * @作者：小蔡
 * @功能介绍：初始化
 */
public abstract class XMvvm extends Application {
    protected String mBaseUrl;
    //界面列表
    private List<Activity> mList = new ArrayList<>();

    public abstract String initBaseUrl();

    public static XMvvm getInstance() {
        return xMvp;
    }

    private static XMvvm xMvp;

    @Override
    public void onCreate() {
        super.onCreate();
        xMvp = this;
        mBaseUrl = initBaseUrl();
        init();
    }

    /**
     * 初始化 application
     */
    public void init() {
        //初始化
        XUtil.initialize(this);
        //设置打印开关
        LogUtil.setIsLog(true);
        //注册Activity生命周期
        registerActivityLifecycleCallbacks(ActivityUtil.getActivityLifecycleCallbacks());
        XBaseRetrofitConfig.setBaseUrl(mBaseUrl);
    }

    /**
     * 添加
     *
     * @param activity
     */
    public void addActivity(Activity activity) {
        if (!mList.contains(activity)) {
            mList.add(activity);
        }
    }

    /**
     * 移除
     *
     * @param activity
     */
    public void removeActivity(Activity activity) {
        if (mList.contains(activity)) {
            mList.remove(activity);
        }
    }

    /**
     * 移除全部app，保留主页
     *
     * @param mactivity
     */
    public void removeActivityShowMain(Activity mactivity) {
        for (Activity activity : mList) {
            if (activity != mactivity)
                activity.finish();
        }
    }

    /**
     * 移除所有
     */
    public void removeAllActivity() {
        for (Activity activity : mList) {
            activity.finish();
        }
    }
}
